home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / _gs / !GS / c / GSIMAGE < prev    next >
Text File  |  1991-10-26  |  24KB  |  754 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gsimage.c */
  21. /* Image procedures for Ghostscript library */
  22. #include "gx.h"
  23. #include "arch.h"
  24. #include "memory_.h"
  25. #include "gserrors.h"
  26. #include "gxfixed.h"
  27. #include "gxarith.h"
  28. #include "gxmatrix.h"
  29. #include "gspaint.h"
  30. #include "gzstate.h"
  31. #include "gzdevice.h"            /* requires gsstate.h */
  32. #include "gzcolor.h"            /* requires gxdevice.h */
  33. #include "gzpath.h"
  34. #include "gximage.h"
  35.  
  36. /* Exported size of enumerator */
  37. const uint gs_image_enum_sizeof = sizeof(gs_image_enum);
  38.  
  39. /* Forward declarations */
  40. private int image_init(P11(gs_image_enum *, int, int, int, int, int,
  41.   gs_matrix *, gs_state *, gx_color_index, gx_color_index, fixed));
  42. /* Procedures for unpacking the input data into 8 bits/sample. */
  43. private void image_unpack_0(iunpack_proc_args);
  44. private void image_unpack_0_spread(iunpack_proc_args);
  45. private void image_unpack_1(iunpack_proc_args);
  46. private void image_unpack_1_spread(iunpack_proc_args);
  47. private void image_unpack_2(iunpack_proc_args);
  48. private void image_unpack_3(iunpack_proc_args);
  49. private void image_unpack_3_spread(iunpack_proc_args);
  50. /* The image_render procedures work on fully expanded, complete rows. */
  51. /* These take a height argument, which is an integer > 0; */
  52. /* they return a negative code, or the number of */
  53. /* rows actually processed (which may be less than the height). */
  54. private int image_render_skip(irender_proc_args);
  55. private int image_render_direct(irender_proc_args);
  56. private int image_render_mono(irender_proc_args);
  57. private int image_render_color(irender_proc_args);
  58.  
  59. /* Start processing an image */
  60. int
  61. gs_image_init(gs_image_enum *penum, gs_state *pgs,
  62.   int width, int height, int bps, int spp, gs_matrix *pmat)
  63. {    int log2_bps, spread;
  64.     if ( pgs->in_cachedevice ) return_error(gs_error_undefined);
  65.     switch ( bps )
  66.        {
  67.     case 1: case 2: case 4:
  68.         log2_bps = bps >> 1;    /* works for 1, 2, 4 */
  69.         break;
  70.     case 8:
  71.         log2_bps = 3;
  72.         break;
  73.     default:
  74.         return_error(gs_error_rangecheck);
  75.        }
  76.     switch ( spp )
  77.        {
  78.     case 1: case 3: case 4:
  79.         spread = 1; break;
  80.     case -3: case -4:
  81.         spp = -spp; spread = spp; break;
  82.     default:
  83.         return_error(gs_error_rangecheck);
  84.        }
  85.     return image_init(penum, width, height, log2_bps, spp, spread,
  86.               pmat, pgs, pgs->device->black, pgs->device->white,
  87.               (fixed)0);
  88. }
  89.  
  90. /* Start processing a masked image */
  91. int
  92. gs_imagemask_init(gs_image_enum *penum, gs_state *pgs,
  93.   int width, int height, int invert, gs_matrix *pmat, int adjust)
  94. {    gx_color_index color0, color1;
  95.     /* The following is wrong for halftones, but */
  96.     /* it doesn't matter, because color0 and color1 */
  97.     /* won't be used if the color isn't pure. */
  98.     if ( invert )
  99.         color0 = gx_no_color_index,
  100.         color1 = pgs->dev_color->color1;
  101.     else
  102.         color0 = pgs->dev_color->color1,
  103.         color1 = gx_no_color_index;
  104.     return image_init(penum, width, height, 0, 1, 1,
  105.               pmat, pgs, color0, color1,
  106.               (adjust && pgs->in_cachedevice ?
  107.                float2fixed(0.25) : (fixed)0));
  108. }
  109.  
  110. /* Common setup for image and imagemask. */
  111. /* Note that the mask tables depend on the end-orientation of the CPU. */
  112. /* We can't simply define them as byte arrays, because */
  113. /* they might not wind up properly long- or short-aligned. */
  114. #define map4tox(a,b,c,d)\
  115.     0, a, b, a+b, c, a+c, b+c, a+b+c,\
  116.     d, a+d, b+d, a+b+d, c+d, a+c+d, b+c+d, a+b+c+d
  117. #if arch_is_big_endian
  118. private unsigned long map_4_to_32[16] =
  119.    {    map4tox(0xffL, 0xff00L, 0xff0000L, 0xff000000L)    };
  120. private unsigned short map_4_to_16[16] =
  121.    {    map4tox(0x55, 0xaa, 0x5500, 0xaa00)    };
  122. #else                    /* !arch_is_big_endian */
  123. private unsigned long map_4_to_32[16] =
  124.    {    map4tox(0xff000000L, 0xff0000L, 0xff00L, 0xffL)    };
  125. private unsigned short map_4_to_16[16] =
  126.    {    map4tox(0x5500, 0xaa00, 0x55, 0xaa)    };
  127. #endif
  128. private int
  129. image_init(register gs_image_enum *penum, int width, int height,
  130.   int log2_bps, int spp, int spread, gs_matrix *pmat, gs_state *pgs,
  131.   gx_color_index color0, gx_color_index color1, fixed adjust)
  132. {    int code;
  133.     gs_matrix mat;
  134.     uint bsize = (width + 8) * spp;    /* round up, +1 for end-of-run byte */
  135.     byte *buffer;
  136.     fixed mtx, mty;
  137.     if ( width <= 0 || height < 0 )
  138.         return_error(gs_error_undefinedresult);
  139.     if ( height == 0 ) return 0;    /* empty image */
  140.     if (    (code = gs_matrix_invert(pmat, &mat)) < 0 ||
  141.         (code = gs_matrix_multiply(&mat, &ctm_only(pgs), &mat)) < 0
  142.        )    return code;
  143.     buffer = (byte *)gs_malloc(1, bsize, "image buffer");
  144.     if ( buffer == 0 ) return_error(gs_error_VMerror);
  145.     penum->width = width;
  146.     penum->height = height;
  147.     penum->log2_bps = log2_bps;
  148.     penum->spp = spp;
  149.     penum->spread = spread;
  150.     penum->fxx = float2fixed(mat.xx);
  151.     penum->fyy = float2fixed(mat.yy);
  152.     if ( (penum->skewed = is_skewed(&mat)) )
  153.        {    penum->fxy = float2fixed(mat.xy);
  154.         penum->fyx = float2fixed(mat.yx);
  155.        }
  156.     else
  157.        {    penum->fxy = 0;
  158.         penum->fyx = 0;
  159.        }
  160.     penum->xcur = mtx = float2fixed(mat.tx);
  161.     penum->ycur = mty = float2fixed(mat.ty);
  162.     penum->pgs = pgs;
  163.     penum->buffer = buffer;
  164.     penum->buffer_size = bsize;
  165.     penum->bytes_per_row =
  166.         (uint)((((ulong)width << log2_bps) * spp / spread + 7) >> 3);
  167.     if ( spp == 1 )
  168.        {    /* Initialize the color table */
  169. #define chtl(i)\
  170.   penum->dev_colors[i].halftone_level
  171.         switch ( log2_bps )
  172.            {
  173.         case 3:
  174.            {    /* Yes, clearing the entire table is slow, */
  175.             /* but for 8 bit-per-sample images, it's worth it. */
  176.             register gx_device_color *pcht = &penum->dev_colors[0];
  177.             register int n = 64;
  178.             do
  179.                {    pcht[0].halftone_level =
  180.                   pcht[1].halftone_level =
  181.                   pcht[2].halftone_level =
  182.                   pcht[3].halftone_level = -1;
  183.                 pcht += 4;
  184.                }
  185.             while ( --n > 0 );
  186.             break;
  187.            }
  188.         case 2:
  189.             chtl(17) = chtl(2*17) = chtl(3*17) =
  190.               chtl(4*17) = chtl(6*17) = chtl(7*17) =
  191.               chtl(8*17) = chtl(9*17) = chtl(11*17) =
  192.               chtl(12*17) = chtl(13*17) = chtl(14*17) = -1;
  193.             /* falls through */
  194.         case 1:
  195.             chtl(5*17) = chtl(10*17) = -1;
  196.         /* 0 doesn't need any further initialization */
  197.            }
  198.         penum->icolor0 = color0;
  199.         chtl(0) = 0;                /* pure color */
  200.         penum->icolor1 = color1;
  201.         chtl(255) = 0;                /* pure color */
  202. #undef chtl
  203.        }
  204.     /* If all four extrema of the image fall within the clipping */
  205.     /* rectangle, clipping is never required. */
  206.        {    gx_path *pcpath = pgs->clip_path;
  207.         fixed xmin = pcpath->cbox.p.x;
  208.         fixed ymin = pcpath->cbox.p.y;
  209.         fixed xmax = pcpath->cbox.q.x;
  210.         fixed ymax = pcpath->cbox.q.y;
  211.         fixed mdx = float2fixed(mat.xx) * width;
  212.         fixed mdy = float2fixed(mat.yy) * height;
  213.         if ( penum->skewed )
  214.            {    mdx += float2fixed(mat.yx) * height;
  215.             mdy += float2fixed(mat.xy) * width;
  216.            }
  217.         penum->never_clip =
  218.             (mdx < 0 ?
  219.                 mtx + mdx >= xmin && mtx <= xmax :
  220.                 mtx >= xmin && mtx + mdx <= xmax) &&
  221.             (mdy < 0 ?
  222.                 mty + mdy >= ymin && mty <= ymax :
  223.                 mty >= ymin && mty + mdy <= ymax);
  224. #ifdef DEBUG
  225. if ( gs_debug['b'] | gs_debug['B'] )
  226.     dprintf4("[b]Image: xmin=%g ymin=%g xmax=%g ymax=%g\n",
  227.          fixed2float(xmin), fixed2float(ymin),
  228.          fixed2float(xmax), fixed2float(ymax)),
  229.     dprintf5("     mtx=%g mty=%g mdx=%g mdy=%g never_clip=%d\n",
  230.          fixed2float(mtx), fixed2float(mty),
  231.          fixed2float(mdx), fixed2float(mdy), penum->never_clip);
  232. #endif
  233.        }
  234.        {    static void (*procs[4])(iunpack_proc_args) = {
  235.             image_unpack_0, image_unpack_1,
  236.             image_unpack_2, image_unpack_3
  237.            };
  238.         static void (*spread_procs[4])(iunpack_proc_args) = {
  239.             image_unpack_0_spread, image_unpack_1_spread,
  240.             image_unpack_2, image_unpack_3_spread
  241.            };
  242.         penum->slow_loop = !penum->never_clip || penum->skewed ||
  243.             /* Use slow loop for imagemask with a halftone */
  244.             ((color0 == gx_no_color_index ||
  245.               color1 == gx_no_color_index) &&
  246.              !color_is_pure(pgs->dev_color));
  247.         penum->render =
  248.             (pgs->in_charpath ? image_render_skip :
  249.              spp > 1 ? image_render_color :
  250.              log2_bps == 0 && !penum->slow_loop &&
  251.              (fixed2long_rounded(mtx + width * penum->fxx) -
  252.               fixed2long(mtx) == width) ?
  253.                image_render_direct : image_render_mono);
  254.         /* If the image is 1-for-1 with the device, */
  255.         /* we don't want to spread the samples. */
  256.         /* The following should just be an assignment of */
  257.         /* a conditional expression, but the Ultrix C compiler */
  258.         /* can't handle it. */
  259.         if ( penum->render == image_render_direct )
  260.           penum->unpack = image_unpack_3;
  261.         else if ( spread != 1 )
  262.           penum->unpack = spread_procs[log2_bps];
  263.         else
  264.           penum->unpack = procs[log2_bps];
  265.        }
  266.     penum->adjust = adjust;
  267.     penum->plane_index = 0;
  268.     penum->byte_in_row = 0;
  269.     penum->y = 0;
  270. #ifdef DEBUG
  271. if ( gs_debug['b'] | gs_debug['B'] )
  272.     dprintf3("[b]Image: w=%d h=%d %s\n",
  273.          width, height, (penum->never_clip ? "no clip" : "must clip")),
  274.     dprintf6("   [%f %f %f %f %f %f]\n",
  275.          mat.xx, mat.xy, mat.yx, mat.yy, mat.tx, mat.ty);
  276. #endif
  277.     return 0;
  278. }
  279.  
  280. /* Process the next piece of an image */
  281. int
  282. gs_image_next(register gs_image_enum *penum, byte *dbytes, uint dsize)
  283. {    uint rsize = penum->bytes_per_row;
  284.     uint pos = penum->byte_in_row;
  285.     int width = penum->width;
  286.     uint dleft = dsize;
  287.     uint dpos = 0;
  288.     int code;
  289.     /* Accumulate separated colors, if needed */
  290.     if ( penum->plane_index == 0 )
  291.         penum->plane_size = dsize;
  292.     else if ( dsize != penum->plane_size )
  293.         return_error(gs_error_undefinedresult);
  294.     penum->planes[penum->plane_index] = dbytes;
  295.     if ( ++(penum->plane_index) != penum->spread )
  296.         return 0;
  297.     penum->plane_index = 0;
  298.     /* We've accumulated an entire set of planes. */
  299.     while ( dleft )
  300.        {    /* Fill up a row, then display it. */
  301.         uint bcount = min(dleft, rsize - pos);
  302.         byte *bptr = penum->buffer + (pos << (3 - penum->log2_bps)) * penum->spread;
  303.         int px;
  304.         for ( px = 0; px < penum->spread; px++ )
  305.             (*penum->unpack)(penum, bptr + px, penum->planes[px] + dpos, bcount);
  306.         pos += bcount;
  307.         dpos += bcount;
  308.         dleft -= bcount;
  309.         if ( pos == rsize )    /* filled an entire row */
  310.            {    code = (*penum->render)(penum, penum->buffer, width * penum->spp, 1);
  311.             if ( code < 0 ) goto err;
  312.             if ( ++(penum->y) == penum->height ) goto end;
  313.             pos = 0;
  314.             penum->xcur += penum->fyx;
  315.             penum->ycur += penum->fyy;
  316.            }
  317.        }
  318.     penum->byte_in_row = pos;
  319.     return 0;
  320. end:    /* End of data */
  321.     code = 1;
  322.     /* falls through */
  323. err:    /* Error, abort */
  324.     gs_free((char *)penum->buffer, penum->buffer_size, 1, "image buffer");
  325.     return code;
  326. }
  327.  
  328. /* ------ Unpacking procedures ------ */
  329.  
  330. private void
  331. image_unpack_0(gs_image_enum *penum, byte *bptr,
  332.   register byte *data, uint dsize)
  333. {    register unsigned long *bufp = (unsigned long *)bptr;
  334.     int left = dsize;
  335.     register unsigned long _ds *map = map_4_to_32;
  336.     while ( left-- )
  337.        {    register unsigned b = *data++;
  338.         *bufp++ = map[b >> 4];
  339.         *bufp++ = map[b & 0xf];
  340.        }
  341. }
  342.  
  343. private void
  344. image_unpack_0_spread(gs_image_enum *penum, register byte *bufp,
  345.   register byte *data, uint dsize)
  346. {    register int spread = penum->spread;
  347.     int left = dsize;
  348.     while ( left-- )
  349.        {    register unsigned b = *data++;
  350.         *bufp = -(b >> 7); bufp += spread;
  351.         *bufp = -((b >> 6) & 1); bufp += spread;
  352.         *bufp = -((b >> 5) & 1); bufp += spread;
  353.         *bufp = -((b >> 4) & 1); bufp += spread;
  354.         *bufp = -((b >> 3) & 1); bufp += spread;
  355.         *bufp = -((b >> 2) & 1); bufp += spread;
  356.         *bufp = -((b >> 1) & 1); bufp += spread;
  357.         *bufp = -(b & 1); bufp += spread;
  358.        }
  359. }
  360.  
  361. private void
  362. image_unpack_1(gs_image_enum *penum, byte *bptr,
  363.   register byte *data, uint dsize)
  364. {    register unsigned short *bufp = (unsigned short *)bptr;
  365.     int left = dsize;
  366.     register unsigned short _ds *map = map_4_to_16;
  367.     while ( left-- )
  368.        {    register unsigned b = *data++;
  369.         *bufp++ = map[b >> 4];
  370.         *bufp++ = map[b & 0xf];
  371.        }
  372. }
  373.  
  374. private void
  375. image_unpack_1_spread(gs_image_enum *penum, register byte *bufp,
  376.   register byte *data, uint dsize)
  377. {    register int spread = penum->spread;
  378.     int left = dsize;
  379.     static byte map_2_to_8[4] = { 0, 0x55, 0xaa, 0xff };
  380.     register byte _ds *map = map_2_to_8;
  381.     while ( left-- )
  382.        {    register unsigned b = *data++;
  383.         *bufp = map[b >> 6]; bufp += spread;
  384.         *bufp = map[(b >> 4) & 3]; bufp += spread;
  385.         *bufp = map[(b >> 2) & 3]; bufp += spread;
  386.         *bufp = map[b & 3]; bufp += spread;
  387.        }
  388. }
  389.  
  390. private void
  391. image_unpack_2(gs_image_enum *penum, register byte *bufp,
  392.   register byte *data, uint dsize)
  393. {    register int spread = penum->spread;
  394.     int left = dsize;
  395.     while ( left-- )
  396.        {    register unsigned b = *data++;
  397.         *bufp = (b & 0xf0) + (b >> 4); bufp += spread;
  398.         b &= 0xf;
  399.         *bufp = (b << 4) + b; bufp += spread;
  400.        }
  401. }
  402.  
  403. private void
  404. image_unpack_3(gs_image_enum *penum, byte *bufp,
  405.   byte *data, uint dsize)
  406. {    if ( data != bufp ) memcpy(bufp, data, dsize);
  407. }
  408.  
  409. private void
  410. image_unpack_3_spread(gs_image_enum *penum, register byte *bufp,
  411.   register byte *data, uint dsize)
  412. {    register int spread = penum->spread;
  413.     register int left = dsize;
  414.     while ( left-- )
  415.        {    *bufp = *data++; bufp += spread;
  416.        }
  417. }
  418.  
  419. /* ------ Rendering procedures ------ */
  420.  
  421. /* Rendering procedure for ignoring an image.  We still need to iterate */
  422. /* over the samples, because the procedure might have side effects. */
  423. private int
  424. image_render_skip(gs_image_enum *penum, byte *data, uint w, int h)
  425. {    return h;
  426. }
  427.  
  428. /* Rendering procedure for a 1-bit-per-pixel sampled image */
  429. /* with no clipping, skewing, rotation, or X scaling. */
  430. /* In this case a direct BitBlt is possible. */
  431. private int
  432. image_render_direct(register gs_image_enum *penum, byte *data, uint w, int h)
  433. {    fixed yt = penum->ycur, yn = yt + penum->fyy;
  434.     int ix = fixed2int_var(penum->xcur);
  435.     int iy = fixed2int_var(yt);
  436.     int ht = fixed2int_var(yn) - iy;
  437.     gx_device *dev = penum->pgs->device->info;
  438.     int (*proc)(P10(gx_device *, byte *, int, int, int, int, int, int, gx_color_index, gx_color_index)) = dev->procs->copy_mono;
  439.     gx_color_index zero = penum->icolor0, one = penum->icolor1;
  440.     if ( ht == 1 )
  441.        {    /* We can do the whole thing at once. */
  442.         (*proc)(dev, data, 0, (w + 7) >> 3,
  443.             ix, iy, w, h, zero, one);
  444.         return h;
  445.        }
  446.     else
  447.        {    /* Do just one row. */
  448.         int dy;
  449.         if ( ht < 0 )
  450.             iy += ht, ht = -ht;
  451.         for ( dy = 0; dy < ht; dy++ )
  452.             (*proc)(dev, data, 0, (w + 7) >> 3,
  453.                 ix, iy + dy, w, 1, zero, one);
  454.         return 1;
  455.        }
  456. }
  457.  
  458. /* Rendering procedure for the general case of displaying a */
  459. /* monochrome image, dealing with multiple bit-per-sample images, */
  460. /* bits not 1-for-1 with the device, clipping, and general transformations. */
  461. /* This procedure handles a single scan line. */
  462. private int
  463. image_render_mono(gs_image_enum *penum, byte *buffer, uint w, int h)
  464. {    gx_color_index zero = penum->icolor0, one = penum->icolor1;
  465.     gs_state *pgs = penum->pgs;
  466.     fixed    dxx = penum->fxx, dxy = penum->fxy,
  467.         dyx = penum->fyx, dyy = penum->fyy;
  468.     gs_fixed_rect cbox;
  469.     int skew = penum->skewed;
  470.     fixed adjust = penum->adjust;
  471.     fixed xa = (dxx >= 0 ? adjust : -adjust);
  472.     fixed xt = penum->xcur;
  473.     fixed ytf = penum->ycur;
  474.     fixed yn;        /* ytf + dyy, if no skew */
  475.     int yt, iht;
  476.     gs_color rcolor;
  477.     gx_device_color *pdevc = pgs->dev_color;
  478.      /* Note: image_set_gray assumes that log2_bps != 0. */
  479. #define image_set_rgb(sample_value)\
  480.   rcolor.luminance = rcolor.red = rcolor.green = rcolor.blue =\
  481.     color_param_from_byte(sample_value)
  482. #define image_set_gray(sample_value)\
  483.    { pdevc = &penum->dev_colors[sample_value];\
  484.      if ( pdevc->halftone_level < 0 )\
  485.       { image_set_rgb(sample_value);\
  486.         gx_color_render(&rcolor, pdevc, pgs);\
  487.       }\
  488.      else\
  489.        gx_color_load(pdevc, pgs);\
  490.    }
  491.     int xcnt = w;
  492.     fixed xl = xt;
  493.     byte *psrc = buffer;
  494.     fixed xrun = xt;        /* x at start of run */
  495.     fixed yrun;            /* y ditto */
  496.     int run = *psrc;        /* run value */
  497.     int htrun =            /* halftone run value */
  498.       (zero == gx_no_color_index ? 255 :
  499.        one == gx_no_color_index ? 0 :
  500.        -2);
  501.     gx_device *dev = pgs->device->info;
  502.     int (*fill_proc)(P6(gx_device *, int, int, int, int, gx_color_index)) = dev->procs->fill_rectangle;
  503.     cbox = pgs->clip_path->bbox;    /* box is known to be up to date */
  504.     if ( !skew )
  505.       { /* Check Y clipping now, and do some extra setup for */
  506.         /* the fast loop as a side effect. */
  507.         int yb;
  508.         if ( dyy > 0 )
  509.           { dyy += adjust << 1;
  510.         ytf -= adjust;
  511.           }
  512.         else
  513.           { dyy = (adjust << 1) - dyy;
  514.         ytf -= dyy - adjust;
  515.           }
  516.         yn = ytf + dyy;
  517.         if ( ytf >= cbox.q.y || yn <= cbox.p.y ) return 1;
  518.         yt = fixed2int_var_rounded(ytf);
  519.         yb = fixed2int_var_rounded(yn);
  520.         iht = yb - yt;
  521.         if ( iht == 0 ) return 1;
  522.       }
  523.     yrun = ytf;
  524.     buffer[w] = ~buffer[w - 1];    /* force end of run */
  525. #ifdef DEBUG
  526. if ( gs_debug['b'] | gs_debug['B'] )
  527.     dprintf5("[b]y=%d w=%d xt=%f yt=%f yb=%f\n",
  528.          penum->y, w,
  529.          fixed2float(xt), fixed2float(ytf), fixed2float(ytf + dyy));
  530. #endif
  531.     rcolor.is_gray = rcolor.luminance_set = 1;
  532.     while ( xcnt-- >= 0 )    /* 1 extra iteration */
  533.                 /* to handle final run */
  534.        {    if ( *psrc++ != run )
  535.            {    /* Fill the region between */
  536.             /* xrun and xl */
  537.             if ( penum->slow_loop )
  538.               { /* Must use general fill */
  539.                 gx_path ipath;
  540.                 gx_path *ppath = &ipath;
  541.                 int code;
  542.                 /* Use halftone if needed. */
  543.                 /* Also handle imagemask here. */
  544.                 if ( run != htrun )
  545.                   { if ( run == 0 )
  546.                   { if ( zero == gx_no_color_index ) goto trans;
  547.                   }
  548.                 else if ( run == 255 )
  549.                   { if ( one == gx_no_color_index ) goto trans;
  550.                   }
  551.                 htrun = run;
  552.                 image_set_gray(run);
  553.                   }
  554.                 if ( !skew )
  555.                   { /* Do the empty/clipping check */
  556.                 /* before going to the trouble of */
  557.                 /* building the path. */
  558.                 /* In the non-skew case, we take the */
  559.                 /* trouble to round the box correctly. */
  560.                 fixed xbr = fixed_rounded(xrun - xa);
  561.                 fixed xer = fixed_rounded(xl + xa);
  562.                 if ( xbr == xer )
  563.                   goto trans;
  564.                 if ( dxx >= 0 )
  565.                   { if ( xbr >= cbox.q.x || xer <= cbox.p.x )
  566.                       goto trans;
  567.                   }
  568.                 else
  569.                   { if ( xer >= cbox.q.x || xbr <= cbox.p.x )
  570.                       goto trans;
  571.                   }
  572.                   }
  573.                 gx_path_init(ppath, &pgs->memory_procs);
  574.                 code = gx_path_add_pgram(ppath,
  575.                              xrun, yrun, xl, ytf,
  576.                              xl + dyx, ytf + dyy);
  577.                 if ( code < 0 )
  578.                   {    gx_path_release(ppath);
  579.                 return code;
  580.                   }
  581.                 if ( skew )
  582.                   {    gs_fixed_rect box;
  583.                 gx_path_bbox(ppath, &box);
  584. #define check_clipped_or_empty(xy, label)\
  585.   if ( box.q.xy <= cbox.p.xy || box.p.xy >= cbox.q.xy ||\
  586.        fixed2int_var_rounded(box.p.xy) == fixed2int_var_rounded(box.q.xy)\
  587.    ) goto label
  588.                 if ( !adjust )
  589.                    {    check_clipped_or_empty(x, nofill);
  590.                     check_clipped_or_empty(y, nofill);
  591.                    }
  592. #undef check_clipped_or_empty
  593.                   }
  594.                 gx_fill_path(ppath, pdevc, pgs,
  595.                      gx_rule_winding_number, adjust);
  596. nofill:                gx_path_release(ppath);
  597. trans:                yrun = ytf;
  598.              }
  599.             else
  600.                {    /* No clipping, no skew, and not */
  601.                 /* imagemask with a halftone. */
  602.                 gx_color_index rcx;
  603.                 int xi = fixed2int_rounded(xrun - xa);
  604.                 int wi = fixed2int_rounded(xl + xa) - xi;
  605. #ifdef DEBUG
  606. if ( gs_debug['B'] )
  607.                 dprintf5("[B]xrun-xa=%g xl+xa=%d y=%d yt=%d iht=%d\n",
  608.                      fixed2float(xrun - xa), fixed2float(xl + xa),
  609.                      penum->y, yt, iht);
  610. #endif
  611.                 if ( wi <= 0 )
  612.                    {    if ( wi == 0 ) goto mt;
  613.                     xi += wi, wi = -wi;
  614.                    }
  615.                 switch ( run )
  616.                    {
  617.                 case 0:
  618.                     rcx = zero;
  619.                     goto zo;
  620.                 case 255:
  621.                     rcx = one;
  622. zo:                    if ( rcx != gx_no_color_index )
  623.                        {    (*fill_proc)(dev, xi, yt, wi, iht, rcx);
  624.                        }
  625.                     break;
  626.                 default:
  627.                     /* Use halftone if needed */
  628.                     if ( run != htrun )
  629.                        {    image_set_gray(run);
  630.                         htrun = run;
  631.                        }
  632.                     gz_fill_rectangle(xi, yt, wi, iht, pdevc, pgs);
  633.                    }
  634. mt: ;               }
  635.             xrun = xl;
  636.             run = psrc[-1];
  637.            }
  638.         xl += dxx;
  639.         ytf += dxy;        /* harmless if no skew */
  640.        }
  641.     return 1;
  642. }
  643.  
  644. /* Rendering procedure for handling color images. */
  645. typedef union { struct { byte r, g, b, skip; } v; ulong all; } color_sample;
  646. private int
  647. image_render_color(gs_image_enum *penum, byte *buffer, uint w, int h)
  648. {    gs_state *pgs = penum->pgs;
  649.     fixed    dxx = penum->fxx, dxy = penum->fxy,
  650.         dyx = penum->fyx, dyy = penum->fyy;
  651.     int skew = penum->skewed;
  652.     fixed xt = penum->xcur;
  653.     fixed ytf = penum->ycur;
  654.     int yt, idy, iht;
  655.     gs_color rcolor;
  656.     gx_device_color devc1, devc2;
  657.     gx_device_color *pdevc = &devc1, *pdevc_next = &devc2;
  658.     int spp = penum->spp;
  659.     fixed xl = xt;
  660.     byte *psrc = buffer;
  661.     fixed xrun = xt;        /* x at start of run */
  662.     fixed yrun = ytf;        /* y ditto */
  663.     color_sample run;        /* run value */
  664.     color_sample next;        /* next sample value */
  665.     byte *bufend = buffer + w;
  666.     bufend[0] = ~bufend[-spp];    /* force end of run */
  667.     if ( !skew )
  668.        {    fixed yn = ytf + dyy;
  669.         yt = fixed2int_var_rounded(ytf);
  670.         iht = fixed2int_var_rounded(yn) - yt;
  671.         if ( iht >= 0 ) idy = 0;
  672.         else idy = iht, iht = -iht;
  673.        }
  674. #ifdef DEBUG
  675. if ( gs_debug['b'] | gs_debug['B'] )
  676.     dprintf5("[b]y=%d w=%d xt=%f yt=%f yb=%f\n",
  677.          penum->y, w,
  678.          fixed2float(xt), fixed2float(ytf), fixed2float(ytf + dyy));
  679. #endif
  680.     run.all = 0;
  681.     next.all = 0;
  682.     rcolor.red = rcolor.green = rcolor.blue = 0;
  683.     gx_color_from_rgb(&rcolor);
  684.     gx_color_render(&rcolor, pdevc, pgs);
  685.     while ( psrc <= bufend )    /* 1 extra iteration */
  686.                 /* to handle final run */
  687.        {    if ( spp == 4 )        /* cmyk */
  688.            {    uint black = 0xff - psrc[3];
  689.             next.v.r = (0xff - psrc[0]) * black / 0xff;
  690.             next.v.g = (0xff - psrc[1]) * black / 0xff;
  691.             next.v.b = (0xff - psrc[2]) * black / 0xff;
  692.             psrc += 4;
  693.            }
  694.         else            /* rgb */
  695.            {    next.v.r = psrc[0];
  696.             next.v.g = psrc[1];
  697.             next.v.b = psrc[2];
  698.             psrc += 3;
  699.            }
  700.         if ( next.all != run.all )
  701.            {    rcolor.red = color_param_from_byte(next.v.r);
  702.             rcolor.green = color_param_from_byte(next.v.g);
  703.             rcolor.blue = color_param_from_byte(next.v.b);
  704.             gx_color_from_rgb(&rcolor);
  705.             gx_color_render(&rcolor, pdevc_next, pgs);
  706.             /* Even though the supplied colors don't match, */
  707.             /* the device colors might. */
  708.             if ( devc1.color1 != devc2.color1 ||
  709.                  devc1.halftone_level != devc2.halftone_level ||
  710.                  (devc1.halftone_level &&
  711.                   devc1.color2 != devc2.color2) ||
  712.                  psrc > bufend    /* force end of last run */
  713.                )
  714.                {    /* Fill the region between */
  715.                 /* xrun and xl */
  716.                 gx_device_color *ptemp;
  717.                 if ( penum->slow_loop )
  718.                {    /* Must use general fill */
  719.                 gx_path ipath;
  720.                 gx_path *ppath = &ipath;
  721.                 int code;
  722.                 gx_path_init(ppath, &pgs->memory_procs);
  723.                 code = gx_path_add_pgram(ppath,
  724.                     xrun, yrun, xl, ytf,
  725.                     xl + dyx, ytf + dyy);
  726.                 if ( code < 0 )
  727.                    {    gx_path_release(ppath);
  728.                     return code;
  729.                    }
  730.                 gx_fill_path(ppath, pdevc, pgs,
  731.                          gx_rule_winding_number, (fixed)0);
  732.                 gx_path_release(ppath);
  733.                }
  734.                 else
  735.                {    /* No clipping, no skew. */
  736.                 int xi = fixed2int_var_rounded(xrun);
  737.                 int wi = fixed2int_var_rounded(xl) - xi;
  738.                 if ( wi < 0 ) xi += wi, wi = -wi;
  739.                 gz_fill_rectangle(xi, yt + idy, wi, iht, pdevc, pgs);
  740.                }
  741.                 xrun = xl;
  742.                 yrun = ytf;
  743.                 ptemp = pdevc;
  744.                 pdevc = pdevc_next;
  745.                 pdevc_next = ptemp;
  746.                }
  747.             run.all = next.all;
  748.            }
  749.         xl += dxx;
  750.         ytf += dxy;        /* harmless if no skew */
  751.        }
  752.     return 1;
  753. }
  754.